<?php


namespace app\user\controller;


use app\common\controller\Common;
use app\common\model\Users;
use wx\Wxapp;

class Instll extends Common
{//小程序获取openid
    public function index()
    {
        $code = request()->param('code');
        if (is_null($code)) return $this->errJson('参数错误');
        $wxapp = new Wxapp();
        $reuser_info = $wxapp->getUser_info($code);
        if ($reuser_info['flag'] == 0) return $this->errJson($reuser_info['msg'], ['flag' => $reuser_info['flag']]);//获取用户信息失败
        $user_info = $reuser_info['data'];
        $user_openid = $user_info['openid'];
        // 判断用户是否存在
        $User = Users::where('openid', '=', $user_openid)->find();
        if (empty($User)) {
            //判断是否注册
            $udatas = [
                'member' => -1,
                'username' => '青檬会员'.rand(11111, 99999),
                'openid' => $user_openid,
                'update_time' => time(),
                'ctime' => time(),
            ];
            $user = new Users();
            $rets = $user->create($udatas);
            if (!$rets) {
                return $this->errJson('请求失败');
            }
        }
        return $this->okJson('成功', ['openid' => $user_openid]);
    }
}